<template>
  <div>
    <el-form-item label="配置信息"></el-form-item>
    <div>
      <div style="width: 100%;display: flex;justify-content: flex-end"><el-button
          type="primary" size="mini" @click="addItemRow">新增配置信息</el-button></div>
      <el-table
          :data="self0ptions.options.inputConfig"
          border
          style="width: 100%;margin-top: 5px"
      >
        <el-table-column
            prop="label"
            label="名称"
            align="center"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.label"></el-input>
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            prop="placeholderContent"
            key="placeholderContent"
            label="内容说明"
        >
          <template slot-scope="scope">
            <el-input v-model="scope.row.placeholderContent"></el-input>
          </template>
        </el-table-column>
        <el-table-column
            align="center"
            prop="handle"
            label="操作"
            width="80px"
        >
          <template slot-scope="scope">
            <el-button type="text" @click="delItem(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
        title="新增配置"
        :visible.sync="dialogVisible"
        width="20%"
        :append-to-body="true"
    >
      <el-form ref="refElForm" :model="formData" :rules="formDataRules" label-width="80px">
        <el-form-item label="名称" prop="label">
          <el-input v-model="formData.label" placeholder="请输入名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="内容说明" prop="placeholderContent">
          <el-input v-model="formData.placeholderContent" placeholder="请输入内容说明" clearable></el-input>
        </el-form-item>
      </el-form>
      <div style="margin-top: 20px;display: flex;justify-content: center">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "inputConfig-editor",
  props:{
    self0ptions:{
      type:Object,
    }
  },
  data(){
    return {
      dialogVisible:false,
      formData:{
        label:'',
        placeholderContent:'',
      },
      formDataRules:{
        label:[{required:true,message:'请填写选项名称',trigger:"blur"}],
      },
    }
  },
  methods:{
    delItem(row){
      this.$confirm('是否确认删除？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const index = this.$props.self0ptions.options.radioConfig.findIndex(item => item.label == row.label)
        if(index > -1){
          this.$props.self0ptions.options.radioConfig.splice(index,1)
          this.$message.success('删除成功')
          return
        }
      })
    },
    addItemRow(){
      this.dialogVisible = true
      this.formData.label = ''
      this.formData.placeholderContent = ''
    },
    submitForm(){
      this.$refs.refElForm.validate(bol=>{
        if(bol){
          this.$props.self0ptions.options.inputConfig.push({
            ...this.formData,
          })
          this.$message.success('添加成功')
          this.dialogVisible = false
        }
      })
    },
  }
}
</script>
<style scoped lang="scss">

</style>